package com.gxuwz.attend.web.servlet.student;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxuwz.attend.context.AppContext;
import com.gxuwz.attend.entity.Leave;
import com.gxuwz.attend.entity.Student;
import com.gxuwz.attend.service.student.LeaveService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/applyLeave")
public class LeaveServlet extends HttpServlet {

    private LeaveService leaveService;


    @Override
    public void init() throws ServletException {
        // 从Spring容器中获取LeaveApplicationService
        leaveService = AppContext.getBean(LeaveService.class);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Student student = (Student) session.getAttribute("currentUser");

        // 设置响应的内容类型为 JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        // 获取请假单列表
        List<Leave> leaveList = leaveService.getLeaves(student.getStuID());

        // 使用 Jackson 的 ObjectMapper，将 leaveList 转换为 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();

              // 将 leaveList 转换为 JSON 字符串
        String leaveListJson = objectMapper.writeValueAsString(leaveList);

        // 返回 JSON 数据
        response.getWriter().write(leaveListJson);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单数据
        String courseID = request.getParameter("courseID");
        String reason = request.getParameter("reason");
        int daynum = Integer.parseInt(request.getParameter("daynum"));
        HttpSession session = request.getSession();
        Student student = (Student) session.getAttribute("currentUser");
        // 调用 Service 层的申请请假方法
        String result = leaveService.applyLeave(courseID, reason, daynum, student.getStuID());

        // 假设 result 是由后端业务逻辑决定的字符串，表示操作结果。
        if ("success".equals(result)) {
            // 使用 JSON 格式响应
            response.setContentType("application/json");
            response.getWriter().write("{\"result\": \"success\", \"message\": \"请假申请提交成功\"}");
        } else {
            // 失败时的响应
            response.setContentType("application/json");
            response.getWriter().write("{\"result\": \"failure\", \"message\": \"请假申请提交失败\"}");
        }

    }


    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String leaveID = request.getParameter("leaveID");
        System.out.println(leaveID);

        // 根据 leaveID 删除记录
        boolean success = leaveService.deleteLeave(leaveID); // 通过服务层删除记录

       if (success) {
           response.setStatus(HttpServletResponse.SC_NO_CONTENT);
       }
    }
}
